<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/layer/layer.js"></script>
<style>
    #txtHint {
        position: absolute;
        top: 260px;
        left: 585px;
        display: none;
        background-color: #EDFAF4;
        min-width: 281px;
        border: 1px solid #aaa;
        border-radius: 5px;
        height: 150px;
        overflow-x: hidden;
    }

    #txtHint li {
        list-style: none;
        color: #333;
        border: 1px solid #EDFAF4;
        padding: 0px 8px;
    }

    #txtHint li:hover {
        color: #c37100;
        background-color: #fdf8d5;
        border: 1px solid #fac900;
        border-radius: 25px;
    }

    #detail span:hover {
        color: #c37100;
    }

    #group {
        position: relative;
    }

    .table1 thead tr th, .table1 tbody tr td {
        text-align: center;
    }
</style>
<script type="text/javascript">
    function huo(i) {
        var a = document.getElementById('q' + i).value = 0;
    }
    function openwin(url) {
        var a = document.createElement("a");
        a.setAttribute("href", url);
        a.setAttribute("target", "_blank");
        a.setAttribute("id", "openwin");
        document.body.appendChild(a);
        a.click();
    }
</script>
<script language="javascript">
    var aa = false;
    function checkSubmit() {
        if (aa == true) {
            return false;//当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
        }
        aa == true;
        return true;
    }
</script>
<style type="text/css">
    .txt1 {
        width: 80px;
        border-radius: 4px;
        border: 1px solid #ccc;
        padding-left: 4px;
    }
    .lab1 {
        width: 100px;
        text-align: right;
        display: inline-block;
        margin-right: 5px;
    }
</style>
            <form action="<?php echo URL::site('receipt/check'); ?>" method="get" class="form-horizontal">
                <div class="control-group">
                <div class="controls">
                    <a class="btn btn-success" href="<?php echo URL::base().'receipt/createbatch'; ?>">生成批次号</a>
                </div>
                </div>
                <div class="control-group">
                    <label class="control-label">卡板批次：</label>
                    <div class="controls">
                    <input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"
                           onafterpaste="this.value=this.value.replace(/\D/g,'')" value="<?php echo isset($batch_code) ? $batch_code : '';?>" name="batch_code" id="curentBatchCode" autocomplete="off" type="text"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">供应商物流号：</label>
                    <div class="controls">
                    <input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"
                           onafterpaste="this.value=this.value.replace(/\D/g,'')" value="<?php echo isset($_GET['track_no']) ? $_GET['track_no'] : '';?>" name="track_no"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">预约单号：</label>
                    <div class="controls">
                    <input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"
                           onafterpaste="this.value=this.value.replace(/\D/g,'')" value="<?php echo isset($purchaseorder) ? $purchaseorder : '';?>" name="purchaseorder"/>
                    </div>
                </div>
                
                <div class="control-group">
                    <div class="controls">
                        <button type="submit" class="btn btn-info">查询</button>

                    </div>
                </div>
            </form>
                    <!-- 显示采购明细表的内容 -->
            <?php if(isset($detail)) { ?>
            <form action="<?php echo URL::site('receipt/receiptinert').'?batch_code='.$batch_code.'&purchaseorder='.$purchaseorder.'&track_no='.$_GET['track_no']; ?>" method="post" id="div1" name="form1"
                                               class="upSuppForm" onsubmit="return checkSubmit()">
                <h3>收货确认操作：</h3>
                <table class="table table1">
                    <thead>
                        <tr style=" background-color:#4EA392;color:#fff;">
                            <th>VMI预约单号</th>
                            <th>VMI预约明细单号</th>
                            <th>计划员</th>
                            <th>跟单员</th>
                            <th>SKU</th>
                            <th>中文名称</th>
                            <th>规格</th>
                            <th>所属仓库</th>
                            <th>sku仓库</th>
                            <th>备注</th>
                            <th>采购数量</th>
                            <th>已经到货数量</th>
                            <th>到货数量</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($detail as $k=>$v){ ?>
                        <tr /*<?php if($k%5==0){}elseif ($k%5==4){echo 'class="success"';}elseif ($k%5==3){echo 'class="error"';}elseif ($k%5==2){echo 'class="warning"';}elseif ($k%5==1){echo 'class="info"';}?>*/>
                            <input type="hidden" value="<?php echo  $v['storage_position'];?>"   name="storage_position[]" />
                            <!-- 采购单号 -->
                            <td>
                                <?php echo  $purchaseorder;?>
                                <input type="hidden" value="<?php echo  $purchaseorder;?>" name="purchaseorder" />
                            </td>
                            <!-- 采购明细单号 -->
                            <td>
                                <?php echo  $v['id'];?>
                                <input type="hidden" value="<?php echo  $v['id'];?>"  name="box_id[]" />
                            </td>
                            <!-- 计划员 -->
                            <td>
                                <?php echo $planer;?>
                                <input type="hidden" value="<?php echo  $planer;?>"  name="planer" />
                            </td>
                            <!-- 跟单员 -->
                            <td>
                                <?php  echo $v['merchandiser'];?>
                                <input type="hidden" value="<?php  echo $v['merchandiser'];?>"  name="merchandiser" />
                            </td>              
                            <!-- SKU -->
                            <td>
                                <input type="hidden" value="<?php echo  $v['sku'];?>" name="sku[]"/><?php echo  $v['sku'];?>
                            </td>                                            
                            <!-- SKU名称 -->
                            <td>
                                <input type="hidden"  value="<?php echo $v['sku_name'];?>" name="sku_name[]" /><?php echo  $v['sku_name'];?>
                            </td>
                            <!-- SKU规格 -->
                            <td>
                                <input type="hidden" value="<?php echo  $v['sku_standard'];?>"name="sku_standard[]" /><?php echo  $v['sku_standard'];?>
                            </td>
                            <!-- 所属仓库 -->
                            <td>
                                <input type="hidden" value="<?php echo  $storeId;?>" name="store" />
                                <span><?php echo $storeName;?></span>
                            </td>
                            <!-- sku仓库 -->
                            <td>
                                <span><?php echo $storeName;?></span>
                            </td>
                            <!-- 备注 -->
                            <td style="color:red;font-weight:bold;">
                                <?php if(isset($remark)){echo $remark;}?>
                            </td>
                            <!-- 采购数量 -->
                            <td>
                                <input class="txt1" type="hidden" id="purchase_count_<?php echo  $v['id'];?>" value="<?php echo  $v['quantity'];?>"name="quantity[]" /><?php echo  $v['quantity'];?>
                            </td>
                            <!-- 已经到货数量 -->
                            <td>
                                <span id="span_<?php echo $v['id'];?>"><?php if(!empty($v['recieve_quantity'])){ echo   $v['recieve_quantity'];}else{echo 0;};?></span>
                            </td>
                            <!-- 到货数量 -->
                            <td>
                                <input class="txt1 focuss" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"   value="<?php  if(($v['quantity']-$v['recieve_quantity'])>0){ echo   $v['quantity']-$v['recieve_quantity'];}else{echo 0;}?>" name="arrival_quantity[]" id="q<?php echo  $v['id'];?>"/>
                            </td>
                            <td>
                                <input class="btn btn-info"  type="button" id="selAll" onclick="huo(<?php echo   $v['id'];?>)" value="清除" />
                            </td>
                        </tr>
                    <?php }?>
                    </tbody>
                    <tfoot>
                    <tr><td colspan="14">
                        <input type="hidden" name="track_no" value="<?php echo isset($_GET['track_no'])?$_GET['track_no']:'' ?>">
                        <input type="submit" class="btn btn-info " id="submit_id" value="收货确认"/></td></tr>
                    </tfoot>
                </table>
            </form>
            <?php }?>

<div style="margin-top:30px;margin-bottom:60px">
    <!-- 显示想要修改的收货数量 -->
    <?php if(isset($print)){ ?>
        <hr/>
        <h2>预约单已收货的明细</h2>
        <table class="table table1">
            <thead>
            <tr>
                <th><input type="checkbox" id="checkAllToAddBatchCode">全选</th>
                <th>VMI预约单号</th>
                <th>收货明细单号</th>
                <th>批次号</th>
                <th>计划员</th>
                <th>跟单员</th>
                <th>SKU</th>
                <th>中文名称</th>
                <th>规格</th>
                <th>所属仓库</th>
                <th>备注</th>
                <th>采购数量</th>
                <th>到货数量</th>
                <th>收货时间</th>
                <th>收货单号</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($print as $v){ ?>
                <tr>
                    <input type="hidden" value="<?php echo $purchaseorder;?>" id="purchaseorder"
                           name="purchaseorder"/>
                    <td>
                        <input type="checkbox" value="<?php echo $v['id'];?>" <?php if($v['batch_code'] == 0) {?>class="tocheck" <?php } else { echo "disabled = 'disabled'";}?>/>
                    </td>
                    <!-- 采购单号 -->
                    <td><?php echo $purchaseorder;?></td>
                    <!-- 收货明细单号 aid-->
                    <td>
                        <?php echo $v['id'] ?>
                    </td>
                    <td>
                        <?php echo $v['batch_code'];?>
                    </td>

                    <!-- 计划员 -->
                    <td>
                        <?php echo $planer;?>
                        <input type="hidden" value="<?php echo  $planer;?>"  name="planer" />
                    </td>
                    <!-- 跟单员 -->
                    <td>
                        <?php  echo comm::getNickByPurchaseId($purchaseorder, $v['sku']);?>
                        <input type="hidden" value=""  name="merchandiser" />
                    </td>   
                    <!-- SKU -->
                    <td>
                        <?php echo $v['sku'] ?>
                    </td>
                    <input type="hidden" value="<?php echo $v['sku'] ?>" id="sku_<?php echo $v['id'] ?>" name="sku"/>

                    <!-- SKU名称 -->
                    <td>
                        <?php echo $v['sku_name'] ?>
                    </td>
                    <!-- SKU 规格-->
                    <td>
                        <?php echo $v['sku_standard'] ?>
                    </td>
                    <!-- 所属仓库-->
                    <td>
                        <span><?php echo $storeName; ?></span>
                    </td>
                    <!-- 备注 -->
                    <td style="color:red;font-weight:bold;"><?php if(isset($remark)){
                            echo $remark;
                        } ?></td>
                    <!-- 采购数量 -->
                    <td>
                        <?php echo $v['purchase_quantity'] ?>
                    </td>
                    <!-- 到货数量 -->
                    <td>
                        <input type="hidden" value="<?php echo $v['arrival_quantity'] ?>"
                               id="jiu_<?php echo $v['id'] ?>"/>
                        <?php if($v['check_count']>0){
                            echo $v['arrival_quantity'];
                        }else{ ?>
                            <input style="width:40px;" onkeyup="this.value=this.value.replace(/\D/g,'')"
                                   onafterpaste="this.value=this.value.replace(/\D/g,'')" type="text"
                                   id="arrival_quantity_<?php echo $v['id']; ?>"
                                   value=" <?php echo $v['arrival_quantity'] ?>" class="test_input change_number"/>
                            <input type="button" id="up_<?php echo $v['id']; ?>" value="修改"
                                   class="btn btn-info myupdate" style="opacity:1;"/>
                        <?php } ?>
                    </td>
                    <!-- 收货时间 -->
                    <td>
                        <?php echo $v['recieve_time']; ?>
                    </td>
                    <!-- 收货单号 aid-->
                    <td>
                        <?php echo $v['recieve_invoice_id'] ?>
                    </td>
                    <td>
                        <!--
    <input type="button btn btn-info" id="mybtn_<?php echo $v['id']; ?>" value="打印" class="btn btn-success" />
     -->
                        <a class="btn btn-success" target="_blank"
                           href="<?php echo URL::base().'printreceipt/skubarcode?str='.$v['recieve_invoice_id'].'&id='.$v['id'].'&sku='.
                               $v['sku'].'&sku_name='.$v['sku_name'].'&sku_standard='.$v['sku_standard'].'&storage_position='.$v['storage_position'].
                               '&quantity='.$v['purchase_quantity'].'&arrival_quantity='.$v['arrival_quantity'].'&recieve_man='.$purchase_id.'&recieve_time='.
                               $v['recieve_time']; ?>">打印</a>
                        <?php if($v['batch_code']==0){?>
                        <a class="btn btn-success"
                           href="<?php echo URL::base().'receipt/addToBatchSingle?recieve_detail_id=' . $v['id'] . '&recieve_invoices_id='.$v['recieve_invoice_id'].'&batch_code='.
                               $batch_code.'&purchaseorder='.$purchaseorder.'&track_no='.$_GET['track_no']; ?>">添加到当前批次</a>
                        <?php }?>
                    </td>
                </tr>
            <?php } ?>
            <tr><td colspan="16" style="text-align: left"><button class="btn btn-info" id="AllToAddBatchCode">批量添加到当前批次</button></td></tr>
            </tbody>
        </table>
    <?php } ?>
</div>
<?php if(isset($recieve_infos)){?>
<h3>批次<?php echo $batch_code;?>详情：</h3>
<table class="table table1">
    <thead>
    <tr>
        <th>收货明细号</th>
        <th>收货时间</th>
        <th>收货人</th>
        <th>预约单号</th>
        <th>计划员</th>
        <th>SKU</th>
        <th>SKU名称</th>
        <th>储位</th>
        <th>采购数量</th>
        <th>收货数量</th>
        <th>质检量</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach($recieve_infos as $recieve_info){?>
        <tr>
            <td>
                <?php echo $recieve_info['id'] ?>
            </td>
            <td>
                <?php echo $recieve_info['recieve_time'] ?>
            </td>
            <td>
                <?php if($recieve_info['recieve_man']) echo comm::getNickByUserId($recieve_info['recieve_man']); ?>
            </td>
            <td>
                <a href="<?php echo url::base(TRUE).'receipt/check?purchaseorder='.$recieve_info['purchaseorder_id'].'&batch_code='.$batch_code  ?>" target="_black"><?php echo $recieve_info['purchaseorder_id'] ?>
            </td>
            <td>
                <?php echo $recieve_info['purchase_man'] ?>
            </td>
            <td>
                <?php echo $recieve_info['sku'] ?>
            </td>
            <td>
                <?php echo $recieve_info['sku_name'] ?>
            </td>
            <td>
                <?php echo $recieve_info['storage_position'] ?>
            </td>
            <td>
                <?php echo $recieve_info['purchase_quantity'] ?>
            </td>
            <td>
                <?php echo $recieve_info['arrival_quantity'] ?>
            </td>
            <td>
                <?php echo $recieve_info['check_count'] ?>
            </td>
        </tr>
    <?php }?>
    </tbody>
    <?php } ?>
</table>
<script type="text/javascript">
    $(document).ready(function () {
        $('.table1 > tbody >tr:nth-child(odd)').css("background-color", "#f5f5f5");
        //当输入的到货数量大于采购数量的时候给予提示
        //$(".focuss").blur(function () {
        $(".focuss").change(function () {
            var quantity = $(this).attr("id");
            var quantity_count = $(this).val();
            var leng = quantity.length;//长度
            var id = quantity.substr(1, leng - 1);//采购明细单号
            var purchase_count = $("#purchase_count_" + id).val();//采购数量
            var arrived = $("#span_" + id).text();//已经到货的采购数量
            var subtract = purchase_count - quantity_count - arrived;

            if (subtract > 0) {
//                layer.alert("你填写的数字比采购数量小" + subtract);
                layer.alert("你填写的数字比采购数量小" + subtract);
            }
            else if (subtract == 0) {
                layer.alert("你填写的数字加上已经到货数量等于采购数量");
            }
            else {
                layer.alert("你填写的数字大于采购数量");
            }
        });

        $("#supplier_id").keyup(function (e) {
            var $val = $(this).val();
            showHint($val);
        });

        $.datepicker.regional['zh-CN'] = {
            closeText: '关闭',
            prevText: '&#x3c;上月',
            nextText: '下月&#x3e;',
            currentText: '今天',
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'],
            monthNamesShort: ['一', '二', '三', '四', '五', '六',
                '七', '八', '九', '十', '十一', '十二'],
            dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
            dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
            dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
            weekHeader: '周',
            dateFormat: 'yy-mm-dd',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: true,
            yearSuffix: '年'
        };

        $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
        $.datepicker.setDefaults({dateFormat: 'yy-mm-dd'});
        $("#j_begintime").datepicker();
        $("#j_endtime").datepicker();
        $("#c_begintime").datepicker();
        $("#c_endtime").datepicker();

        var all_val = new Array();
        var input_values = $(".test_input");
        for (var n = 0; n < input_values.length; n++) {
            var pre = "arrival_quantity_";
            var id = input_values[n].id.substr(pre.length);
            var quantity = $("#arrival_quantity_" + id).val();
            var data = {"id": id, "quantity": quantity};
            all_val.push(data);
        }
        $(".myupdate").attr('disabled', "true");
        $(".test_input").focus(function () {

            var pre = "arrival_quantity_";
            var id = $(this).attr('id').substr(pre.length);//layer.alert("123");
//			for(var i=0;i<all_val.length;i++)
//			{
//				if(all_val[i].id == id)
//				{
//					var before = all_val[i].quantity;
//					break;
//				}
//			}
            var before = $("#jiu_" + id).val();

            $(this).keyup(function () {
                var after = $(this).val().replace(/[ ]/g, "");
                $(this).val(after);

                if (before != after) {
                    $("#up_" + id).removeAttr('disabled');
                }
                else {
                    $("#up_" + id).attr('disabled', "true");
                }
            })
        });

/*         $(".change_number").change(function () {
            var ids = $(this).attr("id");//修改按钮的ID
            var lengs = ids.length;
            var id = ids.substr(17, lengs - 17);//采购明细的ID
            var sku = $("#sku_" + id).val();
            var jiu = $("#jiu_" + id).val();
            var arrival_quantity = $("#arrival_quantity_" + id).val();
            var val = arrival_quantity - jiu;//修改后的到货数量减去之前的到货数量
            if (val < 0) {
                layer.alert("您修改的数字比修改前的数字小" + val);
            }else {
                layer.alert("您修改的数字比修改前的数字大" + val);
            }
        }); */
        $(".myupdate").click(function () {
            var ids = $(this).attr("id");//修改按钮的ID
            var lengs = ids.length;
            var id = ids.substr(3, lengs - 3);//产品的ID
            var sku = $("#sku_" + id).val();
            var jiu = $("#jiu_" + id).val();
            var arrival_quantity = $("#arrival_quantity_" + id).val();
            var purchaseorder = $("#purchaseorder").val();
            var val = arrival_quantity - jiu;//修改后的到货数量减去之前的到货数量
            var notice;

            if (val < 0) {
            	notice = "您修改的数字比修改前的数字小" + val;
            }else {
            	notice = "您修改的数字比修改前的数字小" + val;
            }
    //		var ifnan=isNaN(arrival_quantity);
    //		if(ifnan)
    //		{
    //			layer.alert("你输入的不是数字");
    //			return;
    //		}
            if(confirm(notice)){
                $.ajax({
                    type: 'post',
                    url: '<?php echo url::base(TRUE);?>receipt/receipt_update',
                    dataType: 'html',
                    data: "id=" + id + "&sku=" + sku + "&purchaseorder=" + purchaseorder + "&arrival_quantity=" + arrival_quantity + "&count_val=" + val,
                    async: false,
                    success: function (data) {
                        layer.alert(data);
                        window.location.href = window.location.href;
                        window.location.reload;
                    },
                    error: function () {
                        layer.alert("error");
                    }
                });
            }
        });

        $("#submit_id").click(function () {
            $(this).attr('disabled', true);
            $(".upSuppForm")[0].submit()
        });
    })

    function showHint(str) {
        if (str.length == 0) {
            $("#supplier_id").val('');
            return false;
        }
        var url = "<?php echo URL::base() ?>receipt/select_supplier";
        url += "?name=" + str;
        url += "&sid=" + Math.random();
        $.getJSON(url, function (data) {
            $("#txtHint").html("");
            var $array = data.split(',');
            $.each($array, function (index, value) {
                $('<li>' + value + '</li>').appendTo($("#txtHint"));
                $("#txtHint").show();
                $('#txtHint li').click(function () {
                    $("#supplier_id").val($(this).text());
                    $(this).parent().hide();
                });
            });
        });
    }
    //function quantity_count(ids)
    //{
    //	var ifint=document.getElementById(ids).value;
    //	var re = /^[1-9]\d+$/;
    //	if (!re.test(ifint))
    //    {
    //        layer.alert("请输入整数");
    //        return ;
    //     }
    //}
</script>
<script type="text/javascript">
    $(document).ready(function () {
        var i =<?php if(isset($ifok)){ echo $ifok;?>;
        if (i > 0) {
            layer.alert("确认收货成功");
// 		window.location.href=window.location.href; 
// 		window.location.reload; 
        }
        else {
            layer.alert("收货失败");
            window.location.href = <?php echo URL::base() ?>receipt/index;
            window.location.reload;
        }
        <?php }else{echo 1;}?>

        /*all checked*/
        $("#checkAllToAddBatchCode").click(function(){
            if(this.checked == true){
                $(".tocheck").each(function(){
                    this.checked = true;
                });
            }
            else{
                $(".tocheck").each(function(){
                    this.checked = false;
                });
            }
        });

        $("#AllToAddBatchCode").click(function(){
            var receiptDetailIds = Array();
            var curentBatchCode = $("#curentBatchCode").val();
            if(!curentBatchCode) {
                layer.alert('请填写批次号');
                return false;
            }
            $(".tocheck").each(function(){
                if(this.checked==true){
                    receiptDetailIds.push($(this).val());
                }
            });
            if(!receiptDetailIds.length){layer.alert('请选择');return;}

            $.ajax({
                type:'post',
                url:'<?php echo url::base(true);?>receipt/addToBatchMore',
                dataType:'json',
                data:{'receiptDetailIds' : receiptDetailIds, 'batchCode' : curentBatchCode},
                async:false,
                success:function(data)
                {
                    if(data == 0) {
                        layer.alert("添加失败，非法批次号");
                    } else {
                        layer.alert('添加成功');
                    }
                    window.location.href=window.location.href;
                    window.location.reload;
                },
                error:function(){
                    layer.alert("error");
                }
            });

        });

        $("#curentBatchCode").autocomplete({
            autoFocus: true,
            minLength: 0,
            autoFill: true,
            source: '<?php echo URL::base(true)?>receipt/getBatchCode',
            select: function (event, ui) {
                /**
                 * 从建议列表中选中之后触发的事件
                 */
                $("#curentBatchCode").val(ui.item.name);
                $("#curentBatchCode").attr("readonly", "readonly");
                return false;
            }
        });
    });
</script>
